package com.example.vdfandroidproject;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

	private static int SETTINGS_CODE_RETOUR = 1;
	private static int CHOOSE_MODE_CODE_RETOUR = 2;

	private String userName;
	private TextView timerTextView;
	private TextView timerModeTextView;
	private Button startButton;
	private Button stopButton;

	private CountDownTimer timer;
	private boolean timerStarted;
	private long initialTimerDurationInMillis;
	private long timerDurationInMillis;

	private MediaPlayer mPlayer = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		this.userName = this.getIntent().getExtras().getString("name");

		TextView helloTextView = (TextView) this.findViewById(R.id.helloTextView);
		helloTextView.setText(getString(R.string.hello) + " " + this.userName);

		this.timerTextView = (TextView) this.findViewById(R.id.timerTextView);
		this.startButton = (Button) this.findViewById(R.id.startButton);
		this.stopButton = (Button) this.findViewById(R.id.stopButton);
		this.timerModeTextView = (TextView) this.findViewById(R.id.timerModeTextView);

		timerDurationInMillis = 0;
		this.timerTextView.setText(this.getTimerText(this.timerDurationInMillis));

		this.timerModeTextView.setText(getString(R.string.welcomeMessage));

		startButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (timer != null && timerDurationInMillis > 0) {
					timer.start();
					timerStarted = true;
					playSound(R.raw.klaxon);
				}

			}
		});

		stopButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (timer != null) {
					timer.cancel();
					timerDurationInMillis = 0;
					timerTextView.setText(getTimerText(timerDurationInMillis));
					timer = null;
					timerModeTextView.setText(getString(R.string.welcomeMessage));
					timerStarted = false;
					playSound(R.raw.agneau);
				}
			}
		});

		if (savedInstanceState != null) {
			this.restoreDataFromBundle(savedInstanceState);
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		if (item.getItemId() == R.id.menu_settings) {

			startActivityForResult(new Intent(this, SettingsActivity.class), SETTINGS_CODE_RETOUR);

		} else if (item.getItemId() == R.id.mode_settings) {

			startActivityForResult(new Intent(this, ChooseModeActivity.class), CHOOSE_MODE_CODE_RETOUR);

		}

		return super.onOptionsItemSelected(item);

	}

	private String getTimerText(long millisUntilFinished) {

		long millis = (long) (millisUntilFinished % 1000);
		long seconds = (long) ((millisUntilFinished - millis) / 1000 % 60);
		long minutes = (long) (((millisUntilFinished - millis) / 1000 - seconds) / 60);

		String millisString;
		String secondsString;

		if (millis < 10) {
			millisString = "00" + String.valueOf(millis);
		} else if (millis < 100) {
			millisString = "0" + String.valueOf(millis);
		} else {
			millisString = String.valueOf(millis);
		}

		if (seconds < 10) {
			secondsString = "0" + String.valueOf(seconds);
		} else {
			secondsString = String.valueOf(seconds);
		}

		return String.valueOf(String.valueOf(minutes) + ":" + secondsString + ":" + millisString);

	}

	private class CountDownTimer extends android.os.CountDownTimer {

		public CountDownTimer(long millisInFuture) {
			super(millisInFuture, 49);
		}

		@Override
		public void onFinish() {
			timerTextView.setText(getString(R.string.timeOver));
			timerDurationInMillis = 0;
			playSound(R.raw.agneau);

		}

		@Override
		public void onTick(long millisUntilFinished) {

			timerTextView.setText(getTimerText(millisUntilFinished));
			timerDurationInMillis = millisUntilFinished;

		}

	}

	private void getPreferences() {

		SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);

		((TextView) findViewById(R.id.helloTextView)).setText(getString(R.string.hello) + " "
				+ preferences.getString("login", this.userName));

	}

	private void initializeTimer(long durationInMillis) {
		if (this.timer != null)
			this.timer.cancel();
		
		this.timer = null;
				
		if (durationInMillis > 0) {
			this.timer = new CountDownTimer(durationInMillis);
			this.timerTextView.setText(this.getTimerText(durationInMillis));
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		if (requestCode == SETTINGS_CODE_RETOUR) {

			getPreferences();

		} else if (requestCode == CHOOSE_MODE_CODE_RETOUR) {

			this.timerDurationInMillis = data.getExtras().getLong("DURATION");
			this.initializeTimer(this.timerDurationInMillis);

			String str = data.getExtras().getString("MODE_LABEL");
			if (str != null) {
				this.timerModeTextView.setText(str);
			} else {
				this.timerModeTextView.setText(getString(R.string.welcomeMessage));
			}

		}

		super.onActivityResult(requestCode, resultCode, data);

	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		outState.putLong("DURATION", this.timerDurationInMillis);
		outState.putString("MODE_LABEL", timerModeTextView.getText().toString());
		outState.putBoolean("TIMER_STARTED", this.timerStarted);
		super.onSaveInstanceState(outState);
	}

	private void restoreDataFromBundle(Bundle savedInstanceState) {
		this.timerDurationInMillis = savedInstanceState.getLong("DURATION");
		this.timerTextView.setText(getTimerText(this.timerDurationInMillis));
		this.timerModeTextView.setText(savedInstanceState.getString("MODE_LABEL"));

		if (this.timerDurationInMillis > 0) {
			this.timer = new CountDownTimer(this.timerDurationInMillis);
			if (savedInstanceState.getBoolean("TIMER_STARTED")) {
				this.timer.start();
				this.timerStarted = true;
			} else
				this.timerStarted = false;
		}
	}

	private void playSound(int resId) {
		if (mPlayer != null) {
			mPlayer.stop();
			mPlayer.release();
		}
		mPlayer = MediaPlayer.create(this, resId);
		mPlayer.start();
	}

}
